﻿using AdminBase.Common.Helpers;
using AdminBase.Model;
using AdminBase.Model.Common;
using AdminBase.Model.Entitys;
using AdminBase.Common.Attributes;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using AdminBase.Interface;

namespace AdminBase.webapi.Controllers
{
    /// <summary>
    /// 权限控制器
    /// </summary>
    [ApiController]
    [Authorize]
    [Route("[controller]")]
    [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
    [TypeFilter(typeof(AsyncExceptionFilterAttribute))]
    public class PermissionsController : ControllerBase
    {
        private readonly IPermissionsService _permissions;

        /// <summary>
        /// 构造柱入
        /// </summary>
        /// <param name="permissions"></param>
        public PermissionsController(IPermissionsService permissions)
        {
            _permissions = permissions;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult Add(Permissions req)
        {
            req.CreateUserName = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "FullName")?.Value;
            req.UpdateUserName = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "FullName")?.Value;
            return ResultHelper.Success(_permissions.Add(req));
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult Edit(Permissions req)
        {
            req.UpdateUserName = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "FullName")?.Value;
            return ResultHelper.Success(_permissions.Edit(req));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{id}")]
        public ApiResult Del(long id)
        {
            return ResultHelper.Success(_permissions.Del(id));
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public ApiResult BatchDel([FromBody] List<long> ids)
        {
            return ResultHelper.Success(_permissions.BatchDel(ids));
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{id:long}")]
        public ApiResult GetPermission(long id)
        {
            return ResultHelper.Success(_permissions.GetPermission(id));
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult GetPermissions([FromQuery] PermissionsQueryParam req)
        {
            if (req != null && !string.IsNullOrWhiteSpace(req.GetType) && req.GetType == "tree")
            {
                // 获取所有权限用于树形
                return ResultHelper.Success(_permissions.GetTreeData());
            }
            return ResultHelper.Success(_permissions.GetPermissions(req));
        }

        /// <summary>
        /// 根据用户获取权限
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("u/{id:long}")]
        public ApiResult GetPermissionsByUserID(long id)
        {
            return ResultHelper.Success(_permissions.GetPermissionsIDsByUserID(id));
        }

        /// <summary>
        /// 获取全部菜单权限（树列表）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("menus")]
        public ApiResult GetMenus()
        {
            return ResultHelper.Success(_permissions.GetMenus());
        }

        /// <summary>
        /// 获取权限方法
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("methods")]
        public ApiResult GetPermissionMethods()
        {
            return ResultHelper.Success(_permissions.GetPermissionMethods());
        }
    }
}
